<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Admin;
use Illuminate\Support\Facades\Auth;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Hash;

// 管理员
class AdminController extends Controller
{
    // 列表
    public function index(Request $request)
    {
        $keywords = $request->input('keywords', ''); // 查询关键字

        $data = Admin::query()
            ->when($keywords, function ($query) use ($keywords) {
                $query->where('username', 'like', "%{$keywords}%");
            })
            ->with('admin_role:id,name')
            ->orderByDesc('id')
            ->paginate(env('PAGE_SIZE', 10));
        return showData($data);    
    }

    // 新增
    public function store(Request $request)
    {
        $validate = Validator::make($request->all(), [
            'username' => 'required|max:20',
            'password' => 'required',
            'admin_role_id' => 'required',
            'project_ids' => 'required'
        ], [
            'username.required' => '请输入登录名',
            'password.required' => '请输入密码',
            'admin_role_id.required' => '请输入角色',
            'project_ids.required' => '请输入可选项目ids',
        ]);

        if ($validate->fails()) {
            return showError($validate->errors()->first());
        }

        $data = $validate->getData();

        $lastInfo = Admin::query()->where('username', $data['username'])->first();
        if (!empty($lastInfo) ) {
            return showError('账号已存在');
        }

        $adminInfo['username']=$data['username'];
        $adminInfo['password']=Hash::make($data['password']);
        $adminInfo['admin_role_id']=$data['admin_role_id'];
        $adminInfo['project_id']=explode(',',$data['project_ids'])[0];
        $adminInfo['project_ids']=$data['project_ids'];
        $adminInfo['status']=1;
        try {
            $add=Admin::query()->create($adminInfo);
            return showSuccess('添加成功！');
        } catch (\Exception $e) {
            return showError($e->getMessage());
        }
    }

    // 编辑
    public function update($adminId, Request $request)
    {
        if (empty($adminId)) {
            return showError('id不能为空!');
        }
        // 验证
        $validate = Validator::make($request->all(), [
            'username' => 'required|max:40',
            'admin_role_id' => 'required',
            'project_ids' => 'required',
            'status' => 'required'
        ], [
            'username.required' => '请输入登录名',
            'admin_role_id.required' => '请输入角色',
            'project_ids.required' => '请输入可选项目ids',
            'status.required' => '请输入状态',
        ]);

        if ($validate->fails()) {
            return showError($validate->errors()->first());
        }

        $data = $validate->getData();

        $admin = Admin::query()->where('id', $adminId)->first();
        if (empty($admin)) {
            return showError('用户不存在');
        }

       $lastInfo = Admin::query()->where('username', $data['username'])->first(['id']);
       if (!empty($lastInfo) && $lastInfo['id'] != $adminId) {
           return showError('账号已存在');
       }

        try {
            $adminInfo['username']=$data['username'];
            $adminInfo['admin_role_id']=$data['admin_role_id'];
            $adminInfo['project_ids']=$data['project_ids'];
            $adminInfo['status']=$data['status'];
            $update = Admin::query()->where('id', $adminId)->update($adminInfo);
            return showSuccess('编辑成功！');
        } catch (\Exception $e) {
            return showError($e->getMessage());
        }
    }

    // 删除
    public function destroy($id)
    {
        if (empty($id)) {
            return showError('参数错误！');
        }

        $admin = Admin::query()->where('id', $id)->first();
        if (empty($admin)) {
            return showError('删除项不存在');
        }
        try {
            $delete = Admin::query()->where('id', $id)->delete();
            return showSuccess('删除成功！');
        } catch (\Exception $e) {
            return showError($e->getMessage());
        }
    }

    // 详情
    public function show($id)
    {
        if (empty($id)) {
            return showError('参数错误');
        }
   
        $data = Admin::query()
            ->where('id', $id)
            ->with('admin_role:id,name')
            ->first();

        if (empty($data)) {
            return showError('获取信息失败');
        }

        return showData($data);
    }

      // 切换项目
      public function edit($adminId, Request $request)
      {
          if (empty($adminId)) {
              return showError('id不能为空!');
          }
          // 验证
          $validate = Validator::make($request->all(), [
              'project_id' => 'required',
          ], [
              'project_id.required' => '请输入可选项目id',
          ]);
  
          if ($validate->fails()) {
              return showError($validate->errors()->first());
          }
  
          $data = $validate->getData();
    
          try {
              $adminInfo['project_id']=$data['project_id'];
              $update = Admin::query()->where('id', $adminId)->update($adminInfo);
              return showSuccess('切换成功！');
          } catch (\Exception $e) {
              return showError($e->getMessage());
          }
      }
}

